package com.gzk.hrm.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class FeignRequestInterceptor implements RequestInterceptor {

    private static final String KEY_OF_TOKEN = "Authorization";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取请求
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(requestAttributes!=null&&requestAttributes.getRequest()!=null){
            HttpServletRequest request = requestAttributes.getRequest();

            String token = request.getHeader(KEY_OF_TOKEN);

            requestTemplate.header(KEY_OF_TOKEN,token);
        }

    }
}
